Skip to content

Method: exploreCascaded(Attribute, Object)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model;
19:
20: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
21: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
22: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
23:
24: import java.util.Collection;
25: import java.util.HashSet;
26: import java.util.function.Consumer;
27:
28: public class SimpleOneLevelCascadeExplorer extends OneLevelCascadeExplorer {
29:
30: private final Consumer<Object> cascadedOperation;
31:
32: protected SimpleOneLevelCascadeExplorer(Consumer<Object> cascadedOperation) {
33: this.cascadedOperation = cascadedOperation;
34: }
35:
36: protected void runForEach(final Attribute<?, ?> at, final Object o, boolean cascaded) {
37: Object attVal = EntityPropertiesUtils.getAttributeValue(at, o);
38: if (attVal == null || attVal instanceof LazyLoadingProxy<?>) {
39: return;
40: }
41: if (at.isCollection()) {
42: for (final Object ox2 : new HashSet<>((Collection<?>) attVal)) {
43: if (cascaded) {
44: runCascadedForEach(ox2);
45: } else {
46: runNonCascadedForEach(ox2);
47: }
48: }
49: } else {
50: if (cascaded) {
51: runCascadedForEach(attVal);
52: } else {
53: runNonCascadedForEach(attVal);
54: }
55: }
56: }
57:
58: @Override
59: protected void exploreCascaded(final Attribute<?, ?> at, final Object o) {
60: runForEach(at, o, true);
61: }
62:
63: protected void runCascadedForEach(Object ox2) {
64: cascadedOperation.accept(ox2);
65: }
66:
67: @Override
68: protected void exploreNonCascaded(final Attribute<?, ?> at, final Object o) {
69: runForEach(at, o, false);
70: }
71:
72: protected void runNonCascadedForEach(Object ox2) {
73: // nothing
74: }
75: }